package thread;

/**
 * Created by bao on 2017/8/26.
 */
public class TicketWindow implements Runnable {

    private int tickets = 20;

    @Override
    public void run() {

        while (true) {
            synchronized (this) {
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + "卖出了第" + tickets + "张票。");
                    tickets--;
                } else {
                    System.out.println(Thread.currentThread().getName() + "票卖完了!!!");
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        }


    }
}